Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Side by Side Diff: chrome/common/extensions/docs/server2/local_git_util.py

Issue 1151283007: Docserver overhaul: Gitiles away from me. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove inform_users template to fix presubmit failure (it's now a redirect) Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import os
6 import re
7
8 from file_system import FileNotFoundError
9
10
11 # This provides utilities for extracting information from the local git
12 # repository to which it belongs.
13
14
15 # Regex to match ls-tree output.
16 _LS_TREE_REGEX = re.compile(
17 '\d+\s(?P<type>\w+)\s(?P<id>[0-9a-f]{40})\s(?P<name>.*)')
18
19
20 def _GetRoot():
21 return os.path.join(os.path.dirname(os.path.realpath(__file__)),
22 os.pardir, os.pardir, os.pardir, os.pardir, os.pardir)
23
24
25 def _RelativePath(path):
26 return os.path.join(_GetRoot(), path)
27
28
29 def RunGit(command, args=[]):
30 # We import subprocess symbols lazily because they aren't available on
31 # AppEngine, and the frontend may load (but should never execute) this module.
32 from subprocess import check_output
33 with open(os.devnull, 'w') as dev_null:
34 return check_output(['git', command] + args, stderr=dev_null,
35 cwd=_GetRoot())
36
37
38 def ParseRevision(name):
39 return RunGit('rev-parse', [name]).rstrip()
40
41
42 def GetParentRevision(revision):
43 return RunGit('show', ['-s', '--format=%P', revision]).rstrip()
44
45
46 def GetRootTree(revision):
47 return RunGit('show', ['-s', '--format=%T', revision]).rstrip()
48
49
50 def ListDir(path, revision='HEAD'):
51 '''Retrieves a directory listing for the specified path and optional revision
52 (default is HEAD.) Returns a list of objects with the following properties:
53
54 |type|: the type of entry; 'blob' (file) or 'tree' (directory)
55 |id|: the hash of the directory entry. This is either tree hash or blob hash
56 |name|: the name of the directory entry.
57 '''
58 from subprocess import CalledProcessError
59 try:
60 ref = '%s:%s' % (ParseRevision(revision), path.lstrip('/'))
61 listing = RunGit('ls-tree', [ref]).rstrip().splitlines()
62 except CalledProcessError:
63 raise FileNotFoundError('%s not found in revision %s' % (path, revision))
64
65 def parse_line(line):
66 return re.match(_LS_TREE_REGEX, line).groupdict()
67
68 return map(parse_line, listing)
69
70
71 def ReadFile(path, revision='HEAD'):
72 '''Retrieves the contents of a file at the specified path and optional
73 revision (default is HEAD.) Returns its contents as a string.
74 '''
75 from subprocess import CalledProcessError
76 try:
77 return RunGit('show', ['%s:%s' % (ParseRevision(revision), path)])
78 except CalledProcessError:
79 raise FileNotFoundError('%s not found in revision %s' % (path, revision))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698