Python parentheses coding style fixes

- According to https://www.python.org/dev/peps/pep-0008/

Signed-off-by: Michael Opdenacker <michael.opdenacker@bootlin.com>
This commit is contained in:
Michael Opdenacker 2019-12-01 07:01:26 +01:00
parent fee6c0b5ea
commit 51a6dff682
6 changed files with 258 additions and 258 deletions

View file

@ -17,9 +17,9 @@ def build_query(env, project):
def call_query(query, *args):
cwd = os.getcwd()
os.chdir (ELIXIR_DIR)
os.chdir(ELIXIR_DIR)
ret = query(*args)
os.chdir (cwd)
os.chdir(cwd)
return ret
@ -35,7 +35,7 @@ class IdentResource:
if version == 'latest':
version = call_query(query, 'latest')
symbol_definitions, symbol_references = call_query (query, 'ident', version, ident)
symbol_definitions, symbol_references = call_query(query, 'ident', version, ident)
if len(symbol_definitions) or len(symbol_references):
resp.body = json.dumps(
{

106
data.py
View file

@ -47,21 +47,21 @@ defTypeD = {v: k for k, v in defTypeR.items()}
maxId = 999999999
class DefList:
def __init__ (self, data=b''):
def __init__(self, data=b''):
self.data = data
def iter (self, dummy=False):
for p in self.data.split (b','):
p = re.search (b'(\d*)(\w)(\d*)', p)
def iter(self, dummy=False):
for p in self.data.split(b','):
p = re.search(b'(\d*)(\w)(\d*)', p)
id, type, line = p.groups()
id = int (id)
id = int(id)
type = defTypeR [type.decode()]
line = int (line)
yield (id, type, line)
line = int(line)
yield(id, type, line)
if dummy:
yield (maxId, None, None)
yield(maxId, None, None)
def append (self, id, type, line):
def append(self, id, type, line):
if type not in defTypeD:
return
p = str(id) + defTypeD[type] + str(line)
@ -69,100 +69,100 @@ class DefList:
p = ',' + p
self.data += p.encode()
def pack (self):
def pack(self):
return self.data
class PathList:
def __init__ (self, data=b''):
def __init__(self, data=b''):
self.data = data
def iter (self, dummy=False):
for p in self.data.split (b'\n'):
def iter(self, dummy=False):
for p in self.data.split(b'\n'):
if (p == b''): continue
id, path = p.split (b' ',maxsplit=1)
id = int (id)
id, path = p.split(b' ',maxsplit=1)
id = int(id)
path = path.decode()
yield (id, path)
yield(id, path)
if dummy:
yield (maxId, None)
yield(maxId, None)
def append (self, id, path):
def append(self, id, path):
p = str(id).encode() + b' ' + path
self.data = self.data + p + b'\n'
def pack (self):
def pack(self):
return self.data
class RefList:
def __init__ (self, data=b''):
def __init__(self, data=b''):
self.data = data
def iter (self, dummy=False):
size = len (self.data)
s = BytesIO (self.data)
def iter(self, dummy=False):
size = len(self.data)
s = BytesIO(self.data)
while s.tell() < size:
line = s.readline()
line = line [:-1]
b,c = line.split (b':')
b = int (b.decode())
b,c = line.split(b':')
b = int(b.decode())
c = c.decode()
yield (b, c)
yield(b, c)
s.close()
if dummy:
yield (maxId, None)
yield(maxId, None)
def append (self, id, lines):
def append(self, id, lines):
p = str(id) + ':' + lines + '\n'
self.data += p.encode()
def pack (self):
def pack(self):
return self.data
class BsdDB:
def __init__ (self, filename, readonly, contentType):
def __init__(self, filename, readonly, contentType):
self.filename = filename
self.db = bsddb3.db.DB()
if readonly:
self.db.open (filename, flags=bsddb3.db.DB_RDONLY)
self.db.open(filename, flags=bsddb3.db.DB_RDONLY)
else:
self.db.open (filename,
self.db.open(filename,
flags=bsddb3.db.DB_CREATE,
mode=0o644,
dbtype=bsddb3.db.DB_BTREE)
self.ctype = contentType
def exists (self, key):
key = autoBytes (key)
return self.db.exists (key)
def exists(self, key):
key = autoBytes(key)
return self.db.exists(key)
def get (self, key):
key = autoBytes (key)
p = self.db.get (key)
p = self.ctype (p)
def get(self, key):
key = autoBytes(key)
p = self.db.get(key)
p = self.ctype(p)
return p
def put (self, key, val, sync=False):
key = autoBytes (key)
val = autoBytes (val)
if type (val) is not bytes:
def put(self, key, val, sync=False):
key = autoBytes(key)
val = autoBytes(val)
if type(val) is not bytes:
val = val.pack()
self.db.put (key, val)
self.db.put(key, val)
if sync:
self.db.sync()
class DB:
def __init__ (self, dir, readonly=True):
if os.path.isdir (dir):
def __init__(self, dir, readonly=True):
if os.path.isdir(dir):
self.dir = dir
else:
raise FileNotFoundError
ro = readonly
self.vars = BsdDB (dir + '/variables.db', ro, lambda x: int (x.decode()) )
self.blob = BsdDB (dir + '/blobs.db', ro, lambda x: int (x.decode()) )
self.hash = BsdDB (dir + '/hashes.db', ro, lambda x: x )
self.file = BsdDB (dir + '/filenames.db', ro, lambda x: x.decode() )
self.vers = BsdDB (dir + '/versions.db', ro, PathList)
self.defs = BsdDB (dir + '/definitions.db', ro, DefList)
self.refs = BsdDB (dir + '/references.db', ro, RefList)
self.vars = BsdDB(dir + '/variables.db', ro, lambda x: int(x.decode()) )
self.blob = BsdDB(dir + '/blobs.db', ro, lambda x: int(x.decode()) )
self.hash = BsdDB(dir + '/hashes.db', ro, lambda x: x )
self.file = BsdDB(dir + '/filenames.db', ro, lambda x: x.decode() )
self.vers = BsdDB(dir + '/versions.db', ro, PathList)
self.defs = BsdDB(dir + '/definitions.db', ro, DefList)
self.refs = BsdDB(dir + '/references.db', ro, RefList)

View file

@ -24,9 +24,9 @@ from urllib import parse
realprint = print
outputBuffer = StringIO()
def print (arg, end='\n'):
def print(arg, end='\n'):
global outputBuffer
outputBuffer.write (arg + end)
outputBuffer.write(arg + end)
# Enable CGI Trackback Manager for debugging (https://docs.python.org/fr/3/library/cgitb.html)
import cgitb
@ -41,55 +41,55 @@ ident = ''
status = 200
# Split the URL into its components (project, version, cmd, arg)
m = search ('^/([^/]*)/([^/]*)/([^/]*)(.*)$', os.environ['SCRIPT_URL'])
m = search('^/([^/]*)/([^/]*)/([^/]*)(.*)$', os.environ['SCRIPT_URL'])
if m:
project = m.group (1)
version = m.group (2)
cmd = m.group (3)
arg = m.group (4)
if not (project and search ('^[A-Za-z0-9-]+$', project)) \
or not (version and search ('^[A-Za-z0-9._-]+$', version)):
project = m.group(1)
version = m.group(2)
cmd = m.group(3)
arg = m.group(4)
if not(project and search('^[A-Za-z0-9-]+$', project)) \
or not(version and search('^[A-Za-z0-9._-]+$', version)):
status = 302
location = '/linux/latest/'+cmd+arg
cmd = ''
if cmd == 'source':
path = arg
if len (path) > 0 and path[-1] == '/':
if len(path) > 0 and path[-1] == '/':
path = path[:-1]
status = 301
location = '/'+project+'/'+version+'/source'+path
else:
mode = 'source'
if not search ('^[A-Za-z0-9_/.,+-]*$', path):
if not search('^[A-Za-z0-9_/.,+-]*$', path):
path = 'INVALID'
url = 'source'+path
elif cmd == 'ident':
ident = arg[1:]
form = cgi.FieldStorage()
ident2 = form.getvalue ('i')
ident2 = form.getvalue('i')
if ident == '' and ident2:
status = 302
ident2 = parse.quote(ident2.strip())
location = '/'+project+'/'+version+'/ident/'+ident2
else:
mode = 'ident'
if not (ident and search ('^[A-Za-z0-9_-]*$', ident)):
if not(ident and search('^[A-Za-z0-9_-]*$', ident)):
ident = ''
url = 'ident/'+ident
else:
status = 404
if status == 301:
realprint ('Status: 301 Moved Permanently')
realprint ('Location: '+location+'\n')
realprint('Status: 301 Moved Permanently')
realprint('Location: '+location+'\n')
exit()
elif status == 302:
realprint ('Status: 302 Found')
realprint ('Location: '+location+'\n')
realprint('Status: 302 Found')
realprint('Location: '+location+'\n')
exit()
elif status == 404:
realprint ('Status: 404 Not Found\n')
realprint('Status: 404 Not Found\n')
exit()
basedir = os.environ['LXR_PROJ_DIR']
@ -97,10 +97,10 @@ os.environ['LXR_DATA_DIR'] = basedir + '/' + project + '/data';
os.environ['LXR_REPO_DIR'] = basedir + '/' + project + '/repo';
projects = []
for (dirpath, dirnames, filenames) in os.walk (basedir):
projects.extend (dirnames)
for (dirpath, dirnames, filenames) in os.walk(basedir):
projects.extend(dirnames)
break
projects.sort ()
projects.sort()
import sys
sys.path = [ sys.path[0] + '/..' ] + sys.path
@ -108,14 +108,14 @@ import query
def call_query(*args):
cwd = os.getcwd()
os.chdir ('..')
ret = query.query (*args)
os.chdir (cwd)
os.chdir('..')
ret = query.query(*args)
os.chdir(cwd)
return ret
if version == 'latest':
tag = call_query ('latest')
tag = call_query('latest')
else:
tag = version
@ -130,7 +130,7 @@ data = {
'breadcrumb': '<a class="project" href="'+version+'/source">/</a>'
}
versions = call_query ('versions')
versions = call_query('versions')
v = ''
b = 1
@ -159,40 +159,40 @@ data['versions'] = v
if mode == 'source':
p2 = ''
p3 = path.split ('/') [1:]
p3 = path.split('/') [1:]
links = []
for p in p3:
p2 += '/'+p
links.append ('<a href="'+version+'/source'+p2+'">'+p+'</a>')
links.append('<a href="'+version+'/source'+p2+'">'+p+'</a>')
if links:
data['breadcrumb'] += '/'.join (links)
data['breadcrumb'] += '/'.join(links)
data['ident'] = ident
data['title'] = project.capitalize ()+' source code: '+path[1:]+' ('+tag+') - Bootlin'
data['title'] = project.capitalize()+' source code: '+path[1:]+' ('+tag+') - Bootlin'
lines = ['null - -']
type = call_query ('type', tag, path)
if len (type) > 0:
type = call_query('type', tag, path)
if len(type) > 0:
if type == 'tree':
lines += call_query ('dir', tag, path)
lines += call_query('dir', tag, path)
elif type == 'blob':
content = call_query ('file', tag, path)
content = call_query('file', tag, path)
# Remove the first line of the contents
code = content[content.find ('\n')+1:]
code = content[content.find('\n')+1:]
else:
print ('<div class="lxrerror"><h2>This file does not exist.</h2></div>')
print('<div class="lxrerror"><h2>This file does not exist.</h2></div>')
status = 404
if type == 'tree':
if path != '':
lines[0] = 'back - -'
print ('<div class="lxrtree">')
print ('<table><tbody>\n')
print('<div class="lxrtree">')
print('<table><tbody>\n')
for l in lines:
type, name, size = l.split (' ')
type, name, size = l.split(' ')
if type == 'null':
continue
@ -205,17 +205,17 @@ if mode == 'source':
path2 = path+'/'+name
elif type == 'back':
size = ''
path2 = os.path.dirname (path[:-1])
path2 = os.path.dirname(path[:-1])
if path2 == '/': path2 = ''
name = 'Parent directory'
print (' <tr>\n')
print (' <td><a class="tree-icon icon-'+type+'" href="'+version+'/source'+path2+'">'+name+'</a></td>\n')
print (' <td><a tabindex="-1" class="size" href="'+version+'/source'+path2+'">'+size+'</a></td>\n')
print (' </tr>\n')
print(' <tr>\n')
print(' <td><a class="tree-icon icon-'+type+'" href="'+version+'/source'+path2+'">'+name+'</a></td>\n')
print(' <td><a tabindex="-1" class="size" href="'+version+'/source'+path2+'">'+size+'</a></td>\n')
print(' </tr>\n')
print ('</tbody></table>', end='')
print ('</div>')
print('</tbody></table>', end='')
print('</div>')
elif type == 'blob':
@ -232,27 +232,27 @@ if mode == 'source':
filename = os.path.basename(filename)
def keep_idents(match):
idents.append (match.group (1))
idents.append(match.group(1))
return '__KEEPIDENTS__' + str(len(idents))
def replace_idents(match):
i = idents[int (match.group (1)) - 1]
i = idents[int(match.group(1)) - 1]
return '<a href="'+version+'/ident/'+i+'">'+i+'</a>'
def keep_dtsi(match):
dtsi.append (match.group (4))
return match.group (1) + match.group (2) + match.group (3) + '"__KEEPDTSI__' + str(len(dtsi)) + '"'
dtsi.append(match.group(4))
return match.group(1) + match.group(2) + match.group(3) + '"__KEEPDTSI__' + str(len(dtsi)) + '"'
def replace_dtsi(match):
w = dtsi[int (match.group (1)) - 1]
w = dtsi[int(match.group(1)) - 1]
return '<a href="'+version+'/source'+os.path.dirname(path)+'/'+w+'">'+w+'</a>'
def keep_kconfig(match):
kconfig.append (match.group (4))
return match.group (1) + match.group (2) + match.group (3) + '"__KEEPKCONFIG__' + str(len(kconfig)) + '"'
kconfig.append(match.group(4))
return match.group(1) + match.group(2) + match.group(3) + '"__KEEPKCONFIG__' + str(len(kconfig)) + '"'
def replace_kconfig(match):
w = kconfig[int (match.group (1)) - 1]
w = kconfig[int(match.group(1)) - 1]
return '<a href="'+version+'/source/'+w+'">'+w+'</a>'
ident_filters = {
@ -282,93 +282,93 @@ if mode == 'source':
}
filters = []
filters.append (ident_filters)
filters.append (dtsi_filters)
filters.append (kconfig_filters)
filters.append(ident_filters)
filters.append(dtsi_filters)
filters.append(kconfig_filters)
for f in filters:
c = f['case']
if (c == 'any' or (c == 'filename' and filename in f['match']) or (c == 'extension' and extension in f['match'])):
code = sub (f ['prerex'], f ['prefunc'], code, flags=re.MULTILINE)
code = sub(f ['prerex'], f ['prefunc'], code, flags=re.MULTILINE)
try:
lexer = pygments.lexers.guess_lexer_for_filename (path, code)
lexer = pygments.lexers.guess_lexer_for_filename(path, code)
except:
lexer = pygments.lexers.get_lexer_by_name ('text')
lexer = pygments.lexers.get_lexer_by_name('text')
lexer.stripnl = False
formatter = pygments.formatters.HtmlFormatter (linenos=True, anchorlinenos=True)
result = pygments.highlight (code, lexer, formatter)
formatter = pygments.formatters.HtmlFormatter(linenos=True, anchorlinenos=True)
result = pygments.highlight(code, lexer, formatter)
# Replace line numbers by links to the corresponding line in the current file
result = sub ('href="#-(\d+)', 'name="L\\1" id="L\\1" href="'+version+'/source'+path+'#L\\1', result)
result = sub('href="#-(\d+)', 'name="L\\1" id="L\\1" href="'+version+'/source'+path+'#L\\1', result)
for f in filters:
c = f['case']
if (c == 'any' or (c == 'filename' and filename in f['match']) or (c == 'extension' and extension in f['match'])):
result = sub (f ['postrex'], f ['postfunc'], result)
result = sub(f ['postrex'], f ['postfunc'], result)
print ('<div class="lxrcode">' + result + '</div>')
print('<div class="lxrcode">' + result + '</div>')
elif mode == 'ident':
data['title'] = project.capitalize ()+' source code: '+ident+' identifier ('+tag+') - Bootlin'
data['title'] = project.capitalize()+' source code: '+ident+' identifier ('+tag+') - Bootlin'
symbol_definitions, symbol_references = call_query ('ident', tag, ident)
symbol_definitions, symbol_references = call_query('ident', tag, ident)
print ('<div class="lxrident">')
print('<div class="lxrident">')
if len(symbol_definitions):
print ('<h2>Defined in '+str(len(symbol_definitions))+' files:</h2>')
print ('<ul>')
print('<h2>Defined in '+str(len(symbol_definitions))+' files:</h2>')
print('<ul>')
for symbol_definition in symbol_definitions:
print ('<li><a href="{v}/source/{f}#L{n}"><strong>{f}</strong>, line {n} <em>(as a {t})</em></a>'.format(
print('<li><a href="{v}/source/{f}#L{n}"><strong>{f}</strong>, line {n} <em>(as a {t})</em></a>'.format(
v=version, f=symbol_definition.path, n=symbol_definition.line, t=symbol_definition.type
))
print ('</ul>')
print('</ul>')
print ('<h2>Referenced in '+str(len(symbol_references))+' files:</h2>')
print ('<ul>')
print('<h2>Referenced in '+str(len(symbol_references))+' files:</h2>')
print('<ul>')
for symbol_reference in symbol_references:
ln = symbol_reference.line.split (',')
if len (ln) == 1:
ln = symbol_reference.line.split(',')
if len(ln) == 1:
n = ln[0]
print ('<li><a href="{v}/source/{f}#L{n}"><strong>{f}</strong>, line {n}</a>'.format(
print('<li><a href="{v}/source/{f}#L{n}"><strong>{f}</strong>, line {n}</a>'.format(
v=version, f=symbol_reference.path, n=n
))
else:
if len(symbol_references) > 100: # Concise display
n = len (ln)
print ('<li><a href="{v}/source/{f}"><strong>{f}</strong>, <em>{n} times</em></a>'.format(
n = len(ln)
print('<li><a href="{v}/source/{f}"><strong>{f}</strong>, <em>{n} times</em></a>'.format(
v=version, f=symbol_reference.path, n=n
))
else: # Verbose display
print ('<li><a href="{v}/source/{f}#L{n}"><strong>{f}</strong></a>'.format(
print('<li><a href="{v}/source/{f}#L{n}"><strong>{f}</strong></a>'.format(
v=version, f=symbol_reference.path, n=ln[0]
))
print ('<ul>')
print('<ul>')
for n in ln:
print ('<li><a href="{v}/source/{f}#L{n}">line {n}</a>'.format(
print('<li><a href="{v}/source/{f}#L{n}">line {n}</a>'.format(
v=version, f=symbol_reference.path, n=n
))
print ('</ul>')
print ('</ul>')
print('</ul>')
print('</ul>')
else:
if ident != '':
print ('<h2>Identifier not used</h2>')
print('<h2>Identifier not used</h2>')
status = 404
print ('</div>')
print('</div>')
else:
print ('Invalid request')
print('Invalid request')
if status == 404:
realprint ('Status: 404 Not Found')
realprint('Status: 404 Not Found')
import jinja2
loader = jinja2.FileSystemLoader (os.path.join (os.path.dirname (__file__), '../templates/'))
environment = jinja2.Environment (loader=loader)
template = environment.get_template ('layout.html')
loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), '../templates/'))
environment = jinja2.Environment(loader=loader)
template = environment.get_template('layout.html')
realprint ('Content-Type: text/html;charset=utf-8\n')
realprint('Content-Type: text/html;charset=utf-8\n')
data['main'] = outputBuffer.getvalue()
realprint (template.render(data), end='')
realprint(template.render(data), end='')

36
lib.py
View file

@ -20,12 +20,12 @@
import subprocess, os
def script (*args):
def script(*args):
args = ('./script.sh',) + args
# subprocess.run was introduced in Python 3.5
# fall back to subprocess.check_output if it's not available
if hasattr(subprocess, 'run'):
p = subprocess.run (args, stdout=subprocess.PIPE)
p = subprocess.run(args, stdout=subprocess.PIPE)
p = p.stdout
else:
p = subprocess.check_output(args)
@ -34,20 +34,20 @@ def script (*args):
# Invoke ./script.sh with the given arguments
# Returns the list of output lines
def scriptLines (*args):
p = script (*args)
p = p.split (b'\n')
def scriptLines(*args):
p = script(*args)
p = p.split(b'\n')
del p[-1]
return p
def unescape (bstr):
def unescape(bstr):
subs = (
('\1','\n'),
)
for a,b in subs:
a = a.encode()
b = b.encode()
bstr = bstr.replace (a, b)
bstr = bstr.replace(a, b)
return bstr
# List of tokens which we don't want to consider as identifiers
@ -155,32 +155,32 @@ blacklist = (
b'ptr',
)
def isIdent (bstr):
if len (bstr) < 2:
def isIdent(bstr):
if len(bstr) < 2:
return False
elif bstr in blacklist:
return False
else:
return True
def autoBytes (arg):
if type (arg) is str:
def autoBytes(arg):
if type(arg) is str:
arg = arg.encode()
elif type (arg) is int:
elif type(arg) is int:
arg = str(arg).encode()
return arg
def getDataDir ():
def getDataDir():
try:
dir=os.environ['LXR_DATA_DIR']
except KeyError:
print (argv[0] + ': LXR_DATA_DIR needs to be set')
exit (1)
print(argv[0] + ': LXR_DATA_DIR needs to be set')
exit(1)
return dir
def currentProject ():
return os.path.basename (os.path.dirname (getDataDir ()))
def currentProject():
return os.path.basename(os.path.dirname(getDataDir()))
def hasSupportedExt (filename):
def hasSupportedExt(filename):
ext = os.path.splitext(filename)[1]
return ext.lower() in ['.c', '.cc', '.cpp', '.c++', '.cxx', '.h', '.s']

View file

@ -24,7 +24,7 @@ import data
import os
from collections import OrderedDict
db = data.DB (lib.getDataDir(), readonly=True)
db = data.DB(lib.getDataDir(), readonly=True)
from io import BytesIO
@ -38,11 +38,11 @@ def decode(byte_object):
# decode('ascii') fails on special chars
# FIXME: major hack until we handle everything as bytestrings
try:
return byte_object.decode ('utf-8')
return byte_object.decode('utf-8')
except UnicodeDecodeError:
return byte_object.decode ('iso-8859-1')
return byte_object.decode('iso-8859-1')
def query (cmd, *args):
def query(cmd, *args):
if cmd == 'versions':
# Returns the list of indexed versions in the following format:
@ -50,7 +50,7 @@ def query (cmd, *args):
# Example: v3 v3.1 v3.1-rc10
versions = OrderedDict()
for line in scriptLines ('list-tags', '-h'):
for line in scriptLines('list-tags', '-h'):
taginfo = decode(line).split(' ')
num = len(taginfo)
topmenu, submenu = 'FIXME', 'FIXME'
@ -62,12 +62,12 @@ def query (cmd, *args):
elif (num ==3):
topmenu,submenu,tag = taginfo
if db.vers.exists (tag):
if db.vers.exists(tag):
if topmenu not in versions:
versions[topmenu] = OrderedDict()
if submenu not in versions[topmenu]:
versions[topmenu][submenu] = []
versions[topmenu][submenu].append (tag)
versions[topmenu][submenu].append(tag)
return versions
@ -78,7 +78,7 @@ def query (cmd, *args):
# in the git repository and may not have been indexed yet
# This could results in failed queries
return decode(script ('get-latest')).rstrip('\n')
return decode(script('get-latest')).rstrip('\n')
elif cmd == 'type':
@ -91,7 +91,7 @@ def query (cmd, *args):
version = args[0]
path = args[1]
return decode(script ('get-type', version, path)).strip()
return decode(script('get-type', version, path)).strip()
elif cmd == 'dir':
@ -100,7 +100,7 @@ def query (cmd, *args):
version = args[0]
path = args[1]
entries_str = decode(script ('get-dir', version, path))
entries_str = decode(script('get-dir', version, path))
return entries_str.split("\n")[:-1]
elif cmd == 'file':
@ -112,20 +112,20 @@ def query (cmd, *args):
version = args[0]
path = args[1]
if lib.hasSupportedExt (path):
if lib.hasSupportedExt(path):
buffer = BytesIO()
tokens = scriptLines ('tokenize-file', version, path)
tokens = scriptLines('tokenize-file', version, path)
even = True
for tok in tokens:
even = not even
if even and db.defs.exists (tok) and lib.isIdent (tok):
if even and db.defs.exists(tok) and lib.isIdent(tok):
tok = b'\033[31m' + tok + b'\033[0m'
else:
tok = lib.unescape (tok)
buffer.write (tok)
tok = lib.unescape(tok)
buffer.write(tok)
return decode(buffer.getvalue())
else:
return decode(script ('get-file', version, path))
return decode(script('get-file', version, path))
elif cmd == 'ident':
@ -137,50 +137,50 @@ def query (cmd, *args):
symbol_definitions = []
symbol_references = []
if not db.defs.exists (ident):
if not db.defs.exists(ident):
return symbol_definitions, symbol_references
if not db.vers.exists (version):
if not db.vers.exists(version):
return symbol_definitions, symbol_references
vers = db.vers.get (version).iter()
defs = db.defs.get (ident).iter (dummy=True)
vers = db.vers.get(version).iter()
defs = db.defs.get(ident).iter(dummy=True)
# FIXME: see why we can have a discrepancy between defs and refs
if db.refs.exists (ident):
refs = db.refs.get (ident).iter (dummy=True)
if db.refs.exists(ident):
refs = db.refs.get(ident).iter(dummy=True)
else:
refs = data.RefList().iter (dummy=True)
refs = data.RefList().iter(dummy=True)
id2, type, dline = next (defs)
id3, rlines = next (refs)
id2, type, dline = next(defs)
id3, rlines = next(refs)
dBuf = []
rBuf = []
for id1, path in vers:
while id1 > id2:
id2, type, dline = next (defs)
id2, type, dline = next(defs)
while id1 > id3:
id3, rlines = next (refs)
id3, rlines = next(refs)
while id1 == id2:
dBuf.append ((path, type, dline))
id2, type, dline = next (defs)
dBuf.append((path, type, dline))
id2, type, dline = next(defs)
if id1 == id3:
rBuf.append ((path, rlines))
rBuf.append((path, rlines))
for path, type, dline in sorted (dBuf):
for path, type, dline in sorted(dBuf):
symbol_definitions.append(SymbolInstance(path, dline, type))
for path, rlines in sorted (rBuf):
for path, rlines in sorted(rBuf):
symbol_references.append(SymbolInstance(path, rlines))
return symbol_definitions, symbol_references
else:
return ('Unknown subcommand: ' + cmd + '\n')
return('Unknown subcommand: ' + cmd + '\n')
if __name__ == "__main__":
import sys
output = query (*(sys.argv[1:]))
sys.stdout.buffer.write (output)
output = query(*(sys.argv[1:]))
sys.stdout.buffer.write(output)

112
update.py
View file

@ -25,123 +25,123 @@ import data
import os
from data import PathList
db = data.DB (lib.getDataDir (), readonly=False)
db = data.DB(lib.getDataDir(), readonly=False)
# Store new blobs hashed and file names (without path) for new tag
def updateBlobIDs (tag):
def updateBlobIDs(tag):
if db.vars.exists ('numBlobs'):
idx = db.vars.get ('numBlobs')
if db.vars.exists('numBlobs'):
idx = db.vars.get('numBlobs')
else:
idx = 0
# Get blob hashes and associated file names (without path)
blobs = scriptLines ('list-blobs', '-f', tag)
blobs = scriptLines('list-blobs', '-f', tag)
newBlobs = []
for blob in blobs:
hash, filename = blob.split (b' ',maxsplit=1)
if not db.blob.exists (hash):
db.blob.put (hash, idx)
db.hash.put (idx, hash)
db.file.put (idx, filename)
newBlobs.append (idx)
hash, filename = blob.split(b' ',maxsplit=1)
if not db.blob.exists(hash):
db.blob.put(hash, idx)
db.hash.put(idx, hash)
db.file.put(idx, filename)
newBlobs.append(idx)
idx += 1
db.vars.put ('numBlobs', idx)
db.vars.put('numBlobs', idx)
return newBlobs
def updateVersions (tag):
def updateVersions(tag):
# Get blob hashes and associated file paths
blobs = scriptLines ('list-blobs', '-p', tag)
blobs = scriptLines('list-blobs', '-p', tag)
buf = []
for blob in blobs:
hash, path = blob.split (b' ', maxsplit=1)
idx = db.blob.get (hash)
buf.append ((idx, path))
hash, path = blob.split(b' ', maxsplit=1)
idx = db.blob.get(hash)
buf.append((idx, path))
buf = sorted (buf)
buf = sorted(buf)
obj = PathList()
for idx, path in buf:
obj.append (idx, path)
db.vers.put (tag, obj, sync=True)
obj.append(idx, path)
db.vers.put(tag, obj, sync=True)
def updateDefinitions (blobs):
def updateDefinitions(blobs):
for blob in blobs:
if (blob % 1000 == 0): progress ('defs: ' + str(blob))
hash = db.hash.get (blob)
filename = db.file.get (blob)
if (blob % 1000 == 0): progress('defs: ' + str(blob))
hash = db.hash.get(blob)
filename = db.file.get(blob)
if not lib.hasSupportedExt (filename): continue
if not lib.hasSupportedExt(filename): continue
lines = scriptLines ('parse-defs', hash, filename)
lines = scriptLines('parse-defs', hash, filename)
for l in lines:
ident, type, line = l.split (b' ')
ident, type, line = l.split(b' ')
type = type.decode()
line = int (line.decode())
line = int(line.decode())
if db.defs.exists (ident):
obj = db.defs.get (ident)
if db.defs.exists(ident):
obj = db.defs.get(ident)
else:
obj = data.DefList()
obj.append (blob, type, line)
db.defs.put (ident, obj)
obj.append(blob, type, line)
db.defs.put(ident, obj)
def updateReferences (blobs):
def updateReferences(blobs):
for blob in blobs:
if (blob % 1000 == 0): progress ('refs: ' + str(blob))
hash = db.hash.get (blob)
filename = db.file.get (blob)
if (blob % 1000 == 0): progress('refs: ' + str(blob))
hash = db.hash.get(blob)
filename = db.file.get(blob)
if not lib.hasSupportedExt (filename): continue
if not lib.hasSupportedExt(filename): continue
tokens = scriptLines ('tokenize-file', '-b', hash)
tokens = scriptLines('tokenize-file', '-b', hash)
even = True
lineNum = 1
idents = {}
for tok in tokens:
even = not even
if even:
if db.defs.exists (tok) and lib.isIdent (tok):
if db.defs.exists(tok) and lib.isIdent(tok):
if tok in idents:
idents[tok] += ',' + str(lineNum)
else:
idents[tok] = str(lineNum)
else:
lineNum += tok.count (b'\1')
lineNum += tok.count(b'\1')
for ident, lines in idents.items():
if db.refs.exists (ident):
obj = db.refs.get (ident)
if db.refs.exists(ident):
obj = db.refs.get(ident)
else:
obj = data.RefList()
obj.append (blob, lines)
db.refs.put (ident, obj)
obj.append(blob, lines)
db.refs.put(ident, obj)
def progress (msg):
print ('{} - {} ({:.0%})'.format(project, msg, tagCount/numTags))
def progress(msg):
print('{} - {} ({:.0%})'.format(project, msg, tagCount/numTags))
# Main
tagBuf = []
for tag in scriptLines ('list-tags'):
if not db.vers.exists (tag):
tagBuf.append (tag)
for tag in scriptLines('list-tags'):
if not db.vers.exists(tag):
tagBuf.append(tag)
numTags = len(tagBuf)
tagCount = 0
project = lib.currentProject ()
project = lib.currentProject()
print (project + ' - found ' + str(len(tagBuf)) + ' new tags')
print(project + ' - found ' + str(len(tagBuf)) + ' new tags')
for tag in tagBuf:
tagCount +=1
newBlobs = updateBlobIDs (tag)
progress (tag.decode() + ': ' + str(len(newBlobs)) + ' new blobs')
updateVersions (tag)
updateDefinitions (newBlobs)
updateReferences (newBlobs)
newBlobs = updateBlobIDs(tag)
progress(tag.decode() + ': ' + str(len(newBlobs)) + ' new blobs')
updateVersions(tag)
updateDefinitions(newBlobs)
updateReferences(newBlobs)