From 83d94c93919fe22a045cf1cbc0cc702204ec2d4d Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 19 Aug 2013 09:53:50 +0800 Subject: [PATCH] Added oschina repo download --- doc/bitbucket.go | 6 +-- doc/github.go | 58 +++++---------------- doc/google.go | 6 +-- doc/launchpad.go | 8 ++- doc/oschina.go | 129 +++++++++++++++++++++++++++++++++++++++++++++++ doc/struct.go | 9 ++-- gopm.go | 2 +- 7 files changed, 157 insertions(+), 61 deletions(-) create mode 100644 doc/oschina.go diff --git a/doc/bitbucket.go b/doc/bitbucket.go index e2d96b34f..078ebf017 100644 --- a/doc/bitbucket.go +++ b/doc/bitbucket.go @@ -28,12 +28,12 @@ import ( ) var ( - BitbucketPattern = regexp.MustCompile(`^bitbucket\.org/(?P[a-z0-9A-Z_.\-]+)/(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]*)?$`) + bitbucketPattern = regexp.MustCompile(`^bitbucket\.org/(?P[a-z0-9A-Z_.\-]+)/(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]*)?$`) bitbucketEtagRe = regexp.MustCompile(`^(hg|git)-`) ) -// GetBitbucketDoc downloads tarball from bitbucket.org. -func GetBitbucketDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { +// getBitbucketDoc downloads tarball from bitbucket.org. +func getBitbucketDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { // Check version control. if m := bitbucketEtagRe.FindStringSubmatch(nod.Value); m != nil { match["vcs"] = m[1] diff --git a/doc/github.go b/doc/github.go index e5ef672f5..42f59e55a 100644 --- a/doc/github.go +++ b/doc/github.go @@ -28,7 +28,7 @@ import ( var ( githubRawHeader = http.Header{"Accept": {"application/vnd.github-blob.raw"}} - GithubPattern = regexp.MustCompile(`^github\.com/(?P[a-z0-9A-Z_.\-]+)/(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]*)?$`) + githubPattern = regexp.MustCompile(`^github\.com/(?P[a-z0-9A-Z_.\-]+)/(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]*)?$`) githubCred string ) @@ -42,64 +42,32 @@ func SetGithubCredentials(token string) { } } -// GetGithubDoc downloads tarball from github.com. -func GetGithubDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { +// getGithubDoc downloads tarball from github.com. +func getGithubDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { match["cred"] = githubCred - if nod.Type == BRANCH { + // Check downlaod type. + switch nod.Type { + case BRANCH: if len(nod.Value) == 0 { match["sha"] = MASTER } else { match["sha"] = nod.Value } - } - - // JSON struct for github.com. - var refs []*struct { - Ref string - Url string - Object struct { - Sha string - Type string - Url string - } - } - - if nod.IsGetDeps { - if nod.Type == COMMIT { - // Get up-to-date version. - err := httpGetJSON(client, expand("https://api.github.com/repos/{owner}/{repo}/git/refs?{cred}", match), &refs) - if err != nil { - return nil, err - } - - for _, ref := range refs { - if strings.HasPrefix(ref.Ref, "refs/heads/master") { - match["sha"] = ref.Object.Sha - break - } - } - - nod.Value = match["sha"] - } - } else { - // Check downlaod type. - switch nod.Type { - case TAG, COMMIT, BRANCH: - match["sha"] = nod.Value - default: - return nil, errors.New("Unknown node type: " + nod.Type) - } + case TAG, COMMIT: + match["sha"] = nod.Value + default: + return nil, errors.New("Unknown node type: " + nod.Type) } // We use .zip here. - // zip : https://github.com/{owner}/{repo}/archive/{sha}.zip - // tarball : https://github.com/{owner}/{repo}/tarball/{sha} + // zip: https://github.com/{owner}/{repo}/archive/{sha}.zip + // tarball: https://github.com/{owner}/{repo}/tarball/{sha} // Downlaod archive. p, err := HttpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil) if err != nil { - return nil, err + return nil, errors.New("Fail to donwload Github repo -> " + err.Error()) } shaName := expand("{repo}-{sha}", match) diff --git a/doc/google.go b/doc/google.go index 7147deb99..713c5fd9a 100644 --- a/doc/google.go +++ b/doc/google.go @@ -28,7 +28,7 @@ var ( googleEtagRe = regexp.MustCompile(`^(hg|git|svn)-`) googleFileRe = regexp.MustCompile(`
  • [a-z0-9\-]+)(:?\.(?P[a-z0-9\-]+))?(?P/[a-z0-9A-Z_.\-/]+)?$`) + googlePattern = regexp.MustCompile(`^code\.google\.com/p/(?P[a-z0-9\-]+)(:?\.(?P[a-z0-9\-]+))?(?P/[a-z0-9A-Z_.\-/]+)?$`) ) func setupGoogleMatch(match map[string]string) { @@ -56,8 +56,8 @@ func getGoogleVCS(client *http.Client, match map[string]string) error { return nil } -// GetGoogleDoc downloads raw files from code.google.com. -func GetGoogleDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { +// getGoogleDoc downloads raw files from code.google.com. +func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { setupGoogleMatch(match) // Check version control. if m := googleEtagRe.FindStringSubmatch(nod.Value); m != nil { diff --git a/doc/launchpad.go b/doc/launchpad.go index 4024e2a92..17fdcc7bb 100644 --- a/doc/launchpad.go +++ b/doc/launchpad.go @@ -26,10 +26,10 @@ import ( "strings" ) -var LaunchpadPattern = regexp.MustCompile(`^launchpad\.net/(?P(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]+)*$`) +var launchpadPattern = regexp.MustCompile(`^launchpad\.net/(?P(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]+)*$`) -// GetLaunchpadDoc downloads tarball from launchpad.net. -func GetLaunchpadDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { +// getLaunchpadDoc downloads tarball from launchpad.net. +func getLaunchpadDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { if match["project"] != "" && match["series"] != "" { rc, err := httpGet(client, expand("https://code.launchpad.net/{project}{series}/.bzr/branch-format", match), nil) @@ -101,8 +101,6 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installRepoPa dirs = append(dirs, absPath) } case !strings.HasPrefix(fn, "."): - //os.MkdirAll(path.Dir(absPath)+"/", os.ModePerm) - // Get data from archive. fbytes := make([]byte, h.Size) if _, err := io.ReadFull(tr, fbytes); err != nil { diff --git a/doc/oschina.go b/doc/oschina.go new file mode 100644 index 000000000..1a874958c --- /dev/null +++ b/doc/oschina.go @@ -0,0 +1,129 @@ +// Copyright 2013 gopm authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package doc + +import ( + "archive/zip" + "bytes" + "errors" + "io" + "net/http" + "os" + "regexp" + "strings" +) + +var ( + oscTagRe = regexp.MustCompile(`/repository/archive\?ref=(.*)">`) + oscPattern = regexp.MustCompile(`^git\.oschina\.net/(?P[a-z0-9A-Z_.\-]+)/(?P[a-z0-9A-Z_.\-]+)(?P/[a-z0-9A-Z_.\-/]*)?$`) +) + +// getGithubDoc downloads tarball from git.oschina.com. +func getOSCDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, cmdFlags map[string]bool) ([]string, error) { + // Check downlaod type. + switch nod.Type { + case BRANCH: + if len(nod.Value) == 0 { + match["sha"] = MASTER + } else { + match["sha"] = nod.Value + } + case TAG, COMMIT: + match["sha"] = nod.Value + default: + return nil, errors.New("Unknown node type: " + nod.Type) + } + + // zip: http://{projectRoot}/repository/archive?ref={sha} + + // Downlaod archive. + p, err := HttpGetBytes(client, expand("http://git.oschina.net/{owner}/{repo}/repository/archive?ref={sha}", match), nil) + if err != nil { + return nil, errors.New("Fail to donwload OSChina repo -> " + err.Error()) + } + + suf := "." + nod.Value + if len(suf) == 1 { + suf = "" + } + + projectPath := expand("git.oschina.net/{owner}/{repo}", match) + installPath := installRepoPath + "/" + projectPath + suf + nod.ImportPath = projectPath + + // Remove old files. + os.RemoveAll(installPath + "/") + os.MkdirAll(installPath+"/", os.ModePerm) + + r, err := zip.NewReader(bytes.NewReader(p), int64(len(p))) + if err != nil { + return nil, errors.New("Fail to unzip OSChina repo -> " + err.Error()) + } + + nameLen := len(match["repo"]) + dirs := make([]string, 0, 5) + // Need to add root path because we cannot get from tarball. + dirs = append(dirs, installPath+"/") + for _, f := range r.File { + fileName := f.FileInfo().Name()[nameLen+1:] + absPath := installPath + "/" + fileName + + if strings.HasSuffix(absPath, "/") { + dirs = append(dirs, absPath) + os.MkdirAll(absPath, os.ModePerm) + continue + } + // d, _ := path.Split(absPath) + // if !checkDir(d, dirs) { + // dirs = append(dirs, d) + // os.MkdirAll(d, os.ModePerm) + // } + + // Get file from archive. + rc, err := f.Open() + if err != nil { + return nil, errors.New("Fail to open OSChina repo -> " + err.Error()) + } + + // Write data to file + fw, _ := os.Create(absPath) + if err != nil { + return nil, err + } + + _, err = io.Copy(fw, rc) + // Close files. + rc.Close() + fw.Close() + if err != nil { + return nil, err + } + } + + var imports []string + + // Check if need to check imports. + if nod.IsGetDeps { + for _, d := range dirs { + importPkgs, err := CheckImports(d, match["importPath"]) + if err != nil { + return nil, err + } + imports = append(imports, importPkgs...) + } + } + + return imports, err +} diff --git a/doc/struct.go b/doc/struct.go index 968ddf982..3a1163cb7 100644 --- a/doc/struct.go +++ b/doc/struct.go @@ -69,8 +69,9 @@ type service struct { // services is the list of source code control services handled by gopkgdoc. var services = []*service{ - {GithubPattern, "github.com/", GetGithubDoc}, - {GooglePattern, "code.google.com/", GetGoogleDoc}, - {BitbucketPattern, "bitbucket.org/", GetBitbucketDoc}, - {LaunchpadPattern, "launchpad.net/", GetLaunchpadDoc}, + {githubPattern, "github.com/", getGithubDoc}, + {googlePattern, "code.google.com/", getGoogleDoc}, + {bitbucketPattern, "bitbucket.org/", getBitbucketDoc}, + {launchpadPattern, "launchpad.net/", getLaunchpadDoc}, + {oscPattern, "git.oschina.net/", getOSCDoc}, } diff --git a/gopm.go b/gopm.go index f0b58f8b0..6671acad3 100644 --- a/gopm.go +++ b/gopm.go @@ -37,7 +37,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.2.0.0818" +const APP_VER = "0.2.2.0819" var ( config map[string]interface{}