Skip to content

Instantly share code, notes, and snippets.

@s-knibbs
Last active September 30, 2016 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-knibbs/e3255f3d862da87827d7ae651b9cafb9 to your computer and use it in GitHub Desktop.
Save s-knibbs/e3255f3d862da87827d7ae651b9cafb9 to your computer and use it in GitHub Desktop.
Script to export the installed package list from Chocolatey
"""
export_choco_packages.py - Exports the packages.config file
from the list of locally installed choco packages.
"""
# Copyright (c) 2016 - Simon Knibbs <simon.knibbs@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from jinja2 import Environment
from jinja2.loaders import DictLoader
from distutils.util import strtobool
from subprocess import check_output
import argparse
import re
import os
XML_NAME = "packages.config"
XML_TEMPLATE = """\
<?xml version="1.0" encoding="utf-8"?>
<packages>
{% for pkg in packages %}
<package id="{{ pkg.name }}"{% if versions %} version="{{ pkg.version }}"{% endif %} />
{% endfor %}
</packages>
"""
def parse_args():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'--include-versions', '-I', metavar='y|n|true|false|1|0',
type=strtobool, default=True, help='Include package versions.'
)
parser.add_argument(
'--out-dir', '-o', default='.',
help='Output directory.'
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
env = Environment(
loader=DictLoader({XML_NAME: XML_TEMPLATE}),
trim_blocks=True,
lstrip_blocks=True
)
choco_out = check_output(['choco', 'list', '--localonly'])
package_re = re.compile(r'([\w.\-]+) (\d[\d.]*)')
packages = []
for line in choco_out.splitlines():
match = package_re.match(line)
# Ignore chocolatey package and other lines in output
if match and match.group(1).lower() != 'chocolatey':
packages.append({'name': match.group(1), 'version': match.group(2)})
with open(os.path.join(args.out_dir, XML_NAME), 'w') as tmpl_file:
tmpl = env.get_template(XML_NAME)
tmpl_file.write(
tmpl.render(packages=packages, versions=args.include_versions)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment