Exemple #1
0
 def test_jsonlinting():
     """Verify correct json syntax."""
     for i in glob.glob(os.path.join(DOC_DIR, 'Documents', '*.json')):
         assert demjson.jsonlint('jsonlint').main([i]) == 0, (
             'SSM Autmation JSON documents are not well formed')
     for i in glob.glob(os.path.join(DOC_DIR, 'Tests', 'CloudFormationTemplates' '*.json')):
         assert demjson.jsonlint('jsonlint').main([i]) == 0, (
             'CF Template JSON documents are not well formed')
Exemple #2
0
    def _json_loader(filename):
        try:
            with open(filename, 'rb') as data:
                load.update(json.load(data))
        except ValueError:
            if jsonlint:
                with open(filename, 'rb') as data:
                    lint = jsonlint()
                    rc = lint.main(['-v', filename])

            logger.critical('Error with configuration file')
            sys.exit(-1)
Exemple #3
0
    def _json_loader(filename):
        try:
            with open(filename, 'rb') as data:
                load.update(json.load(data))
        except ValueError:
            if jsonlint:
                with open(filename, 'rb') as data:
                    lint = jsonlint()
                    rc = lint.main(['-v', filename])

            logger.critical('Error with configuration file')
            sys.exit(-1)
Exemple #4
0
    def _load_config(self):
        if not os.path.isfile(self.tags_cfg_file):
            msg = "Tags file does not exists: %s"
            self.logger.error(msg, self.tags_cfg_file)
            return

        jsonlint_args = ['--verbose', '--strict']
        for k, v in DEMJSON_ARGS.items():
            if k.startswith('allow_') and v is True:
                jsonlint_args.append('--allow=' + k[len('allow_'):])
        jsonlint_args.append(self.tags_cfg_file)

        to_log = cStringIO.StringIO()
        ret = jsonlint(stdout=to_log, stderr=to_log).main(jsonlint_args)
        if ret:
            to_log.seek(0)
            self.logger.error("Invalid JSON file %s", self.tags_cfg_file)
            self.logger.error(to_log.read())
        else:
            with open(self.tags_cfg_file, 'r') as fd:
                self.tags = decode(fd.read(), **DEMJSON_ARGS)
Exemple #5
0
 def test_jsonlinting():
     """Verify correct json syntax."""
     for i in glob.glob(os.path.join(DOC_DIR, 'Documents', '*.json')):
         assert demjson.jsonlint('jsonlint').main([i]) == 0, (
             'JSON documents are not well formed')
Exemple #6
0
def handle_file(prop, f):
    for p in list(prop.items()):
        print('key:{0} value:{1}'.format(p[0], p[1]))
    print('-' * 30)
    name = prop.get('Name', '')
    if (len(name) == 0):
        if 'File' in prop: name = prop['File']
        else:
            print("Entry without name")
            return
    prop.pop('Name')
    write_attr(f, QUERY_BASE + 1, name.encode(arg['enc']))
    filename = prop.get('File', '')
    if len(filename) == 0:
        #if 'Location' in prop: prop['Content-Location']=prop.pop('Location') # это перенаправление файл и не нужен
        if not 'Content-Location' in prop:
            filename = name  # если нет имени файла считаем им имя ресурса
    else:
        prop.pop('File')
    if 'Authorize' in prop:
        prop['Authorization'] = prop['Authorize']
        prop.pop('Authorize')
    # к этому моменту в словаре исключительно заголовки. Имя ресурса и имя файла ресурса исключены
    for p in list(prop.items()):
        if p[0] == 'Last-Modified':  # обработка даты
            dt = datetime.datetime.strptime(
                p[1].split['GMT'][0].strip(),
                "%a, %d %b %Y %H:%M:%S") - datetime.datetime(2000, 1, 1)
            f.write(struct.pack("IB", 4, keys[p[0]]))
            f.write(struct.pack("IBBB", int(dt.total_seconds()), 0, 0, 0))
        elif p[0] == 'ETag':  # обработка ETag
            f.write(struct.pack("IB", 4, keys[p[0]]))
            f.write(struct.pack("IBBB", int(p[1], 16), 0, 0, 0))
        elif p[0] == 'Authorization':
            value = 1
            if p[1].upper() == 'FALSE': value = 0
            f.write(struct.pack("IBBBB", 1, keys['Authorization'], value, 0,
                                0))
        else:  # обработка текстовых атрибутов
            if (p[0] in keys):
                write_attr(f, keys[p[0]], p[1].encode(arg['enc']))
            else:
                print("Unknown HTML attribute ({0}) in {'1'} name".format(
                    p[0], name))
                return
    if ('Content-Location' in prop) or ('Location' in prop):
        return  # для перенаправления остальное не нужно
    # считывание файла целиком (он не может быть большой, или не поместится в кассу)
    filedata = get_filedata(filename)
    if filename.endswith('.json') and check_json:
        lint = demjson.jsonlint(program_name=sys.argv[0])
        rc = lint.main(filename)
        if rc != 0: json_errors = True
    if arg['minify']:
        ext = is_minified(filename)
        if ext != 0:
            print('using minification for ', name)
            minifname = 'minifname.' + ext
            open(minifname, 'wb').write(filedata)
            outputname = 'minified.min.' + ext
            comppath = os.path.dirname(os.path.realpath(__file__))
            if ext == 'js':
                os.system('java -jar ' + comppath +
                          '/yuicompressor-2.4.6.jar %s -o %s --charset utf-8' %
                          (minifname, outputname))
            elif ext == 'css':
                os.system('java -jar ' + comppath +
                          '/yuicompressor-2.4.6.jar %s -o %s --charset utf-8' %
                          (minifname, outputname))
            else:
                os.system(
                    'java -jar ' + comppath +
                    '/htmlcompressor-1.5.3.jar --compress-js --compress-css -o %s %s --charset utf-8'
                    % (outputname, minifname))
            if os.path.exists(outputname):
                fileout = open(outputname, 'rb').read()
                if len(fileout):
                    print('minify success')
                    filedata = fileout
                os.remove(outputname)
            os.remove(minifname)
    if 'Content-Encoding' in prop:
        if prop['Content-Encoding'] == 'deflate':
            filedata = zlib.compress(filedata)
        elif prop['Content-Encoding'] == 'gzip':
            gzf = gzip.open(filename + '.gz', 'wb')
            gzf.write(filedata)
            gzf.close()
            filedata = open(filename + '.gz', 'rb').read()
            os.remove(filename + '.gz')
    elif arg['autocompress']:
        zdata = zlib.compress(filedata)
        if len(filedata) - len(zdata) > len(filedata) / 10:
            filedata = zdata
            print('using autocompress for ', name)
            write_attr(f, keys['Content-Encoding'], b'deflate')
    # передача дополнительных атрибутов
    if (not 'ETag' in prop) and arg['etag']:
        f.write(struct.pack("IB", 4, keys['ETag']))
        f.write(struct.pack("IBBB", zlib.crc32(filedata), 0, 0, 0))
        print('calculate ETag for ', name)
    if (not 'Content-MD5' in prop) and arg['md5']:
        write_attr(f, keys['Content-MD5'],
                   base64.encodestring(hashlib.md5(filedata).digest()).strip())
        print('calculate md5 for ', name)
    if (not 'Last-Modified' in prop) and arg['filedata']:
        dt = get_filetmstamp(filename) - datetime.datetime(2000, 1, 1)
        f.write(struct.pack("IB", 4, keys['Last-Modified']))
        f.write(struct.pack("IBBB", int(dt.total_seconds()), 0, 0, 0))
        print('set modification time for ', name)

    # передача файла
    write_body(f, QUERY_BASE + 5, filedata)
#!/usr/bin/env python
"""
Checks JSON files for errors. Recursively scans current dir.

requires: demjson
"""

import os

import demjson

linter = demjson.jsonlint()

for root, dirs, files in os.walk('.'):
    for name in files:
        filename = os.path.join(root, name)
        if filename.endswith('.json'):
            # print(filename)
            linter.main([filename])