mirror of https://github.com/gogits/gogs.git
Unknown
12 years ago
6 changed files with 190 additions and 74 deletions
@ -0,0 +1,141 @@
|
||||
// Copyright (c) 2013 GPMGo Members. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package doc |
||||
|
||||
import ( |
||||
"archive/tar" |
||||
"bytes" |
||||
"compress/gzip" |
||||
"io" |
||||
"net/http" |
||||
"os" |
||||
"path" |
||||
"regexp" |
||||
"strings" |
||||
) |
||||
|
||||
var LaunchpadPattern = regexp.MustCompile(`^launchpad\.net/(?P<repo>(?P<project>[a-z0-9A-Z_.\-]+)(?P<series>/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(?P<dir>/[a-z0-9A-Z_.\-/]+)*$`) |
||||
|
||||
// GetLaunchpadDoc downloads tarball from launchpad.net.
|
||||
func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH, commit string, cmdFlags map[string]bool) (*Package, []string, error) { |
||||
|
||||
if match["project"] != "" && match["series"] != "" { |
||||
rc, err := httpGet(client, expand("https://code.launchpad.net/{project}{series}/.bzr/branch-format", match), nil) |
||||
switch { |
||||
case err == nil: |
||||
rc.Close() |
||||
// The structure of the import path is launchpad.net/{root}/{dir}.
|
||||
case isNotFound(err): |
||||
// The structure of the import path is is launchpad.net/{project}/{dir}.
|
||||
match["repo"] = match["project"] |
||||
match["dir"] = expand("{series}{dir}", match) |
||||
default: |
||||
return nil, nil, err |
||||
} |
||||
} |
||||
|
||||
// bundle and snapshot will have commit 'B' and 'S',
|
||||
// but does not need to download dependencies.
|
||||
isCheckImport := len(commit) == 0 |
||||
|
||||
var downloadPath string |
||||
// Check if download with specific revision.
|
||||
if isCheckImport || len(commit) == 1 { |
||||
downloadPath = expand("https://bazaar.launchpad.net/+branch/{repo}/tarball", match) |
||||
} else { |
||||
downloadPath = expand("https://bazaar.launchpad.net/+branch/{repo}/tarball/"+commit, match) |
||||
} |
||||
|
||||
// Scrape the repo browser to find the project revision and individual Go files.
|
||||
p, err := httpGetBytes(client, downloadPath, nil) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
installPath := installGOPATH + "/src/" + match["importPath"] |
||||
|
||||
// Remove old files.
|
||||
os.RemoveAll(installPath + "/") |
||||
// Create destination directory.
|
||||
os.MkdirAll(installPath+"/", os.ModePerm) |
||||
|
||||
gzr, err := gzip.NewReader(bytes.NewReader(p)) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
defer gzr.Close() |
||||
|
||||
tr := tar.NewReader(gzr) |
||||
|
||||
var autoPath string // Auto path is the root path that generated by bitbucket.org.
|
||||
// Get source file data.
|
||||
dirs := make([]string, 0, 5) |
||||
for { |
||||
h, err := tr.Next() |
||||
if err == io.EOF { |
||||
break |
||||
} else if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
fn := h.FileInfo().Name() |
||||
// Check root path.
|
||||
if len(autoPath) == 0 { |
||||
autoPath = fn[:strings.Index(fn, match["repo"])+len(match["repo"])] |
||||
} |
||||
absPath := strings.Replace(fn, autoPath, installPath, 1) |
||||
|
||||
// Check if it is a directory.
|
||||
if h.FileInfo().IsDir() { |
||||
// Directory.
|
||||
// Check if current directory is example.
|
||||
if !(!cmdFlags["-e"] && strings.Contains(absPath, "example")) { |
||||
dirs = append(dirs, absPath) |
||||
} |
||||
continue |
||||
} |
||||
|
||||
// Create diretory before create file.
|
||||
os.MkdirAll(path.Dir(absPath)+"/", os.ModePerm) |
||||
|
||||
// Get data from archive.
|
||||
fbytes := make([]byte, h.Size) |
||||
if _, err := io.ReadFull(tr, fbytes); err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
// Write data to file
|
||||
fw, err := os.Create(absPath) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
_, err = fw.Write(fbytes) |
||||
fw.Close() |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
} |
||||
|
||||
pkg := &Package{ |
||||
ImportPath: match["importPath"], |
||||
AbsPath: installPath, |
||||
Commit: commit, |
||||
} |
||||
|
||||
var imports []string |
||||
|
||||
// Check if need to check imports.
|
||||
if isCheckImport { |
||||
for _, d := range dirs { |
||||
imports, err = checkImports(d+"/", match["importPath"]) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
} |
||||
} |
||||
|
||||
return pkg, imports, err |
||||
} |
Loading…
Reference in new issue