Skip to content

Instantly share code, notes, and snippets.

@evrardjp
Created March 22, 2017 16:02
Show Gist options
  • Save evrardjp/8e1180f18cd4581b118e23babe2602ce to your computer and use it in GitHub Desktop.
Save evrardjp/8e1180f18cd4581b118e23babe2602ce to your computer and use it in GitHub Desktop.
packages_file.py
# 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.
#
# (c) 2017, Jean-Philippe Evrard <[email protected]>
# Take a path to a debian mirror release file, and outputs a list with:
# - package name
# - package version
# - checksum
# - Relative location to pool folder
#
# example:
# get_url:
# url: http://rpc-repo.rackspace.com/apt-mirror/integrated/dists/r14.0.0rc1-trusty/main/binary-amd64/Packages
# dest: /tmp/trusty-amd64-Packages
# debug:
# var: item
# with_packages_file:
# - /tmp/trusty-amd64-Packages
import os
from ansible.plugins.lookup import LookupBase
from ansible.errors import AnsibleLookupError
IMPORTANT_FIELDS = ['Version', 'Filename', 'MD5sum', 'SHA1', 'SHA256']
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
def parse_fields(line):
for field in IMPORTANT_FIELDS:
if line.startswith(field + ":"):
return (field, line.split(":")[1].strip())
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
ret = []
for term in terms:
pkg_details = {}
with open(term, 'r') as f:
for line in f:
#non empty line means pkg data
if line.strip():
if line.startswith('Package:'):
currentpkg = line.split(":")[1].strip()
pkg_details[currentpkg] = {}
else:
# Now doing package data
if line.startswith('Version:'):
pkg_details[currentpkg]['Version'] = line.split(":")[1].strip()
elif line.startswith('Filename:'):
pkg_details[currentpkg]['Filename'] = line.split(":")[1].strip()
elif line.startswith('MD5sum:'):
pkg_details[currentpkg]['MD5sum'] = line.split(":")[1].strip()
else:
currentpkg=""
ret.append(pkg_details)
return ret
# For debug purposes
if __name__ == '__main__':
import sys
import json
print(json.dumps(LookupModule().run(terms=sys.argv[1:]), indent=4, sort_keys=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment