Skip to content

Commit

Permalink
Merge pull request #11 from Senzing/issue-3.dockter.1
Browse files Browse the repository at this point in the history
Shipped with SenzingAPI 2.4.0
  • Loading branch information
docktermj authored Jun 7, 2021
2 parents 861de05 + 328425a commit 5b81752
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
[markdownlint](https://dlaa.me/markdownlint/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.7.0] - 2021-02-24

### Added to 1.7.0

- Shipped with SenzingAPI 2.4.0

## [1.6.0] - 2020-12-15

### Added to 1.6.0
Expand Down
103 changes: 55 additions & 48 deletions G2CreateProject.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
#! /usr/bin/env python3

import argparse
import errno
import json
import os
import shutil
import sys
import textwrap
from pathlib import Path


def find_replace_in_file(filename, old_string, new_string):
''' Replace strings in new project files '''

try:
with open(filename) as f:
s = f.read()
except IOError as ex:
raise ex

try:
with open(filename, 'w') as f:
with open(filename) as fr:
s = fr.read()
with open(filename, 'w') as fw:
s = s.replace(old_string, new_string)
f.write(s)
fw.write(s)
except IOError as ex:
raise ex

Expand All @@ -32,90 +27,102 @@ def get_version():
version = build_version = None

try:
with open(os.path.join(senzing_path, 'g2BuildVersion.json')) as file_version:
build_version_file = senzing_path.joinpath('g2BuildVersion.json')
with open(build_version_file) as file_version:
version_details = json.load(file_version)
except Exception as ex:
pass
else:
version = version_details.get('VERSION', None)
build_version = version_details.get('BUILD_VERSION', None)
except IOError as ex:
print(f'\nERROR: Unable to read {build_version_file} to retrieve version details!')
print(f' {ex}')
# Exit if version details can't be read. g2BuildVersion.json should be available and is copied to project
return False

return f'{version} - ({build_version})' if version and build_version else 'Unknown, error reading build details!'
version = version_details.get('VERSION', None)
build_version = version_details.get('BUILD_VERSION', None)

return f'{version} - ({build_version})' if version and build_version else 'Unable to determine version!'


def get_ignored(path, filenames):
''' Return list of paths/files to ignore for copying '''

ret = []

for filename in filenames:
if os.path.join(path, filename) in paths_to_exclude:
if Path(path).joinpath(filename) in paths_to_exclude:
ret.append(filename)
elif filename in files_to_exclude:
ret.append(filename)

return ret


if __name__ == '__main__':

senzing_path = '/opt/senzing/g2'
senz_install_path = '/opt/senzing'
# senzing_path on normal rpm/deb install = /opt/senzing/g2
# senzing_install_root would then = /opt/senzing
senzing_path = Path(__file__).resolve().parents[1]
senz_install_root = Path(__file__).resolve().parents[2]

version = get_version()
if not version:
sys.exit(1)

# Example: paths_to_exclude = [senzing_path.joinpath('python')]
paths_to_exclude = []
files_to_exclude = ['G2CreateProject.py', 'G2UpdateProject.py']

parser = argparse.ArgumentParser(description='Create a per-user instance of Senzing in a new folder with the specified name.')
parser.add_argument('folder', metavar='F', nargs='?', default='~/senzing', help='the name of the folder to create, it must not already exist (Default: %(default)s)')
parser = argparse.ArgumentParser(description='Create a new instance of a Senzing project in a path')
parser.add_argument('path', metavar='PATH', nargs='?', default='~/senzing', help='path to create new Senzing project in, it must not already exist (Default: %(default)s)')
args = parser.parse_args()

target_path = os.path.normpath(os.path.join(os.getcwd(), os.path.expanduser(args.folder)))
if os.path.exists(target_path) or os.path.isfile(target_path):
print(f'\n{target_path} already exists or is a file. Please specify a folder that does not already exist.')
target_path = Path(args.path).expanduser().resolve()

if target_path.exists() and target_path.samefile(senz_install_root):
print(f'\nProject cannot be created in {senz_install_root}. Please specify a different path.')
sys.exit(1)

if target_path.startswith(senz_install_path):
print(f'\nProject cannot be created in {senz_install_path}. Please specify a different folder.')
if target_path.exists() or target_path.is_file():
print(f'\n{target_path} already exists or is a file. Please specify a path that does not already exist.')
sys.exit(1)

print(textwrap.dedent(f'''\n\
Creating Senzing instance at: {target_path}
Senzing version: {get_version()}
Senzing version: {version}
'''))

# Copy senzing_path to new project path
shutil.copytree(senzing_path, target_path, ignore=get_ignored)

# Copy resources/templates to etc
files_to_ignore = shutil.ignore_patterns('G2C.db', 'setupEnv', '*.template')
shutil.copytree(os.path.join(senzing_path,'resources', 'templates'), os.path.join(target_path, 'etc'), ignore=files_to_ignore)

##project_etc_path = os.path.join(target_path, 'etc')
shutil.copytree(senzing_path.joinpath('resources', 'templates'), target_path.joinpath('etc'), ignore=files_to_ignore)

# Copy setupEnv
shutil.copyfile(os.path.join(senzing_path, 'resources', 'templates', 'setupEnv'), os.path.join(target_path, 'setupEnv'))
shutil.copyfile(senzing_path.joinpath('resources', 'templates', 'setupEnv'), target_path.joinpath('setupEnv'))

# Copy G2C.db to runtime location
os.makedirs(os.path.join(target_path, 'var', 'sqlite'))
shutil.copyfile(os.path.join(senzing_path, 'resources', 'templates', 'G2C.db'), os.path.join(target_path, 'var', 'sqlite','G2C.db'))
# Soft link in data
os.symlink('/opt/senzing/data/1.0.0', os.path.join(target_path, 'data'))
Path.mkdir(target_path.joinpath('var', 'sqlite'), parents=True)
shutil.copyfile(senzing_path.joinpath('resources', 'templates', 'G2C.db'), target_path.joinpath('var', 'sqlite', 'G2C.db'))

# Soft link data
target_path.joinpath('data').symlink_to(senz_install_root.joinpath('data', '1.0.0'))

# Files to modify in new project
# Files & strings to modify in new project
files_to_update = [
'setupEnv',
'etc/G2Module.ini'
target_path.joinpath('setupEnv'),
target_path.joinpath('etc', 'G2Module.ini'),
]

senzing_path_subs = [
('${SENZING_DIR}', target_path),
('${SENZING_CONFIG_PATH}', os.path.join(target_path, 'etc')),
('${SENZING_DATA_DIR}', os.path.join(target_path, 'data')),
('${SENZING_RESOURCES_DIR}', os.path.join(target_path, 'resources')),
('${SENZING_VAR_DIR}', os.path.join(target_path, 'var'))
('${SENZING_CONFIG_PATH}', target_path.joinpath('etc')),
('${SENZING_DATA_DIR}', target_path.joinpath('data')),
('${SENZING_RESOURCES_DIR}', target_path.joinpath('resources')),
('${SENZING_VAR_DIR}', target_path.joinpath('var'))
]

for f in files_to_update:
for p in senzing_path_subs:
find_replace_in_file(os.path.join(target_path, f), p[0], p[1])
find_replace_in_file(f, p[0], str(p[1]))

print('Succesfully created.')

0 comments on commit 5b81752

Please sign in to comment.