Code cleanup.

This commit is contained in:
Martin Edenhofer 2016-01-21 23:25:05 +01:00
parent 5e518f5037
commit 6d6779caf7
2 changed files with 90 additions and 90 deletions

View file

@ -19,7 +19,7 @@ class Index extends App.ControllerContent
url: "#{@apiPath}/packages",
processData: true,
success: (data) =>
@packages = data
@packages = data.packages
@render()
)
@ -52,7 +52,7 @@ class Index extends App.ControllerContent
id: 'packages'
type: httpType
url: "#{@apiPath}/packages",
data: JSON.stringify( { id: id } )
data: JSON.stringify(id: id)
processData: false
success: =>
@load()

View file

@ -6,17 +6,17 @@ class Package < ApplicationModel
# build package based on .szpm
# Package.build(
# :file => 'package.szpm',
# :root => '/path/to/src/extention/',
# :output => '/path/to/package_location/'
# file: 'package.szpm',
# root: '/path/to/src/extention/',
# output: '/path/to/package_location/'
# )
def self.build(data)
if data[:file]
xml = _read_file( data[:file], data[:root] || true )
xml = _read_file(data[:file], data[:root] || true)
package = _parse(xml)
elsif data[:string]
package = _parse( data[:string] )
package = _parse(data[:string])
end
build_date = REXML::Element.new('build_date')
@ -24,19 +24,19 @@ class Package < ApplicationModel
build_host = REXML::Element.new('build_host')
build_host.text = Socket.gethostname
package.root.insert_after( '//zpm/description', build_date )
package.root.insert_after( '//zpm/description', build_host )
package.root.insert_after('//zpm/description', build_date)
package.root.insert_after('//zpm/description', build_host)
package.elements.each('zpm/filelist/file') do |element|
location = element.attributes['location']
content = _read_file( location, data[:root] )
content = _read_file(location, data[:root])
base64 = Base64.encode64(content)
element.text = base64
end
if data[:output]
location = data[:output] + '/' + package.elements['zpm/name'].text + '-' + package.elements['zpm/version'].text + '.zpm'
location = "#{data[:output]}/#{package.elements['zpm/name'].text}-#{package.elements['zpm/version'].text}.zpm"
logger.info "NOTICE: writting package to '#{location}'"
file = File.new( location, 'wb' )
file.write( package.to_s )
file = File.new(location, 'wb')
file.write(package.to_s)
file.close
return true
end
@ -46,16 +46,16 @@ class Package < ApplicationModel
# Package.auto_install
# install all packages located under auto_install/*.zpm
def self.auto_install
path = @@root + '/auto_install/'
return if !File.exist?( path )
path = "#{@@root}/auto_install/"
return if !File.exist?(path)
data = []
Dir.foreach( path ) do |entry|
Dir.foreach(path) do |entry|
if entry =~ /\.zpm/ && entry !~ /^\./
data.push entry
end
end
data.each {|file|
install( file: path + '/' + file )
install(file: "#{path}/#{file}")
}
data
end
@ -65,15 +65,15 @@ class Package < ApplicationModel
# note: will not take down package migrations, use Package.unlink instead
def self.unlink_all
# link files
Dir.glob( @@root + '/**/*' ) do |entry|
if File.symlink?( entry)
Dir.glob("#{@@root}/**/*") do |entry|
if File.symlink?(entry)
logger.info "unlink: #{entry}"
File.delete( entry )
File.delete(entry)
end
backup_file = entry + '.link_backup'
if File.exist?( backup_file )
if File.exist?(backup_file)
logger.info "Restore backup file of #{backup_file} -> #{entry}."
File.rename( backup_file, entry )
File.rename(backup_file, entry)
end
end
end
@ -81,8 +81,8 @@ class Package < ApplicationModel
# check if zpm is a package source repo
def self._package_base_dir?(package_base_dir)
package = false
Dir.glob( package_base_dir + '/*.szpm') do |entry|
package = entry.sub( %r{^.*/(.+?)\.szpm$}, '\1')
Dir.glob(package_base_dir + '/*.szpm') do |entry|
package = entry.sub(%r{^.*/(.+?)\.szpm$}, '\1')
end
if package == false
fail "Can't link package, '#{package_base_dir}' is no package source directory!"
@ -99,24 +99,24 @@ class Package < ApplicationModel
package = _package_base_dir?(package_base_dir)
# migration down
Package::Migration.migrate( package, 'reverse' )
Package::Migration.migrate(package, 'reverse')
# link files
Dir.glob( package_base_dir + '/**/*' ) do |entry|
entry = entry.sub( '//', '/' )
Dir.glob(package_base_dir + '/**/*') do |entry|
entry = entry.sub('//', '/')
file = entry
file = file.sub( /#{package_base_dir.to_s}/, '' )
file = file.sub(/#{package_base_dir.to_s}/, '')
dest = @@root + '/' + file
if File.symlink?( dest.to_s )
if File.symlink?(dest.to_s)
logger.info "Unlink file: #{dest}"
File.delete( dest.to_s )
File.delete(dest.to_s)
end
backup_file = dest.to_s + '.link_backup'
if File.exist?( backup_file )
if File.exist?(backup_file)
logger.info "Restore backup file of #{backup_file} -> #{dest}."
File.rename( backup_file, dest.to_s )
File.rename(backup_file, dest.to_s)
end
end
end
@ -129,11 +129,11 @@ class Package < ApplicationModel
package = _package_base_dir?(package_base_dir)
# link files
Dir.glob( package_base_dir + '/**/*' ) do |entry|
entry = entry.sub( '//', '/' )
Dir.glob(package_base_dir + '/**/*') do |entry|
entry = entry.sub('//', '/')
file = entry
file = file.sub( /#{package_base_dir.to_s}/, '' )
file = file.sub( %r{^/}, '' )
file = file.sub(/#{package_base_dir.to_s}/, '')
file = file.sub(%r{^/}, '')
# ignore files
if file =~ /^README/
@ -144,44 +144,44 @@ class Package < ApplicationModel
# get new file destination
dest = @@root + '/' + file
if File.directory?( entry.to_s )
if !File.exist?( dest.to_s )
if File.directory?(entry.to_s)
if !File.exist?(dest.to_s)
logger.info "Create dir: #{dest}"
FileUtils.mkdir_p( dest.to_s )
FileUtils.mkdir_p(dest.to_s)
end
end
if File.file?( entry.to_s ) && ( File.file?( dest.to_s ) && !File.symlink?( dest.to_s ) )
if File.file?(entry.to_s) && (File.file?(dest.to_s) && !File.symlink?(dest.to_s))
backup_file = dest.to_s + '.link_backup'
if File.exist?( backup_file )
if File.exist?(backup_file)
fail "Can't link #{entry} -> #{dest}, destination and .link_backup already exists!"
else
logger.info "Create backup file of #{dest} -> #{backup_file}."
File.rename( dest.to_s, backup_file )
File.rename(dest.to_s, backup_file)
end
end
if File.file?( entry )
if File.symlink?( dest.to_s )
File.delete( dest.to_s )
if File.file?(entry)
if File.symlink?(dest.to_s)
File.delete(dest.to_s)
end
logger.info "Link file: #{entry} -> #{dest}"
File.symlink( entry.to_s, dest.to_s )
File.symlink(entry.to_s, dest.to_s)
end
end
# migration up
Package::Migration.migrate( package )
Package::Migration.migrate(package)
end
# Package.install( :file => '/path/to/package.zpm' )
# Package.install( :string => zpm_as_string )
# Package.install(file: '/path/to/package.zpm')
# Package.install(string: zpm_as_string)
def self.install(data)
if data[:file]
xml = _read_file( data[:file], true )
xml = _read_file(data[:file], true)
package = _parse(xml)
elsif data[:string]
package = _parse( data[:string] )
package = _parse(data[:string])
end
# package meta data
@ -195,13 +195,13 @@ class Package < ApplicationModel
}
# verify if package can get installed
package_db = Package.find_by( name: meta[:name] )
package_db = Package.find_by(name: meta[:name])
if package_db
if !data[:reinstall]
if Gem::Version.new( package_db.version ) == Gem::Version.new( meta[:version] )
if Gem::Version.new(package_db.version) == Gem::Version.new(meta[:version])
fail "Package '#{meta[:name]}-#{meta[:version]}' already installed!"
end
if Gem::Version.new( package_db.version ) > Gem::Version.new( meta[:version] )
if Gem::Version.new(package_db.version) > Gem::Version.new(meta[:version])
fail "Newer version (#{package_db.version}) of package '#{meta[:name]}-#{meta[:version]}' already installed!"
end
end
@ -215,13 +215,13 @@ class Package < ApplicationModel
end
# store package
record = Package.create( meta )
record = Package.create(meta)
if !data[:reinstall]
Store.add(
object: 'Package',
o_id: record.id,
data: package.to_s,
filename: meta[:name] + '-' + meta[:version] + '.zpm',
filename: "#{meta[:name]}-#{meta[:version]}.zpm",
preferences: {},
created_by_id: UserInfo.current_user_id || 1,
)
@ -241,32 +241,32 @@ class Package < ApplicationModel
record.save
# up migrations
Package::Migration.migrate( meta[:name] )
Package::Migration.migrate(meta[:name])
# prebuild assets
true
end
# Package.reinstall( package_name )
# Package.reinstall(package_name)
def self.reinstall(package_name)
package = Package.find_by( name: package_name )
package = Package.find_by(name: package_name)
if !package
fail "No such package '#{package_name}'"
end
file = _get_bin( package.name, package.version )
install( string: file, reinstall: true )
file = _get_bin(package.name, package.version)
install(string: file, reinstall: true)
end
# Package.uninstall( :name => 'package', :version => '0.1.1' )
# Package.uninstall( :string => zpm_as_string )
def self.uninstall( data )
# Package.uninstall(name: 'package', version: '0.1.1')
# Package.uninstall(string: zpm_as_string)
def self.uninstall(data)
if data[:string]
package = _parse( data[:string] )
package = _parse(data[:string])
else
file = _get_bin( data[:name], data[:version] )
file = _get_bin(data[:name], data[:version])
package = _parse(file)
end
@ -278,7 +278,7 @@ class Package < ApplicationModel
# down migrations
if !data[:migration_not_down]
Package::Migration.migrate( meta[:name], 'reverse' )
Package::Migration.migrate(meta[:name], 'reverse')
end
package.elements.each('zpm/filelist/file') do |element|
@ -304,7 +304,7 @@ class Package < ApplicationModel
def self._parse(xml)
logger.debug xml.inspect
begin
package = REXML::Document.new( xml )
package = REXML::Document.new(xml)
rescue => e
logger.error 'ERROR: ' + e.inspect
return
@ -313,7 +313,7 @@ class Package < ApplicationModel
package
end
def self._get_bin( name, version )
def self._get_bin(name, version)
package = Package.find_by(
name: name,
version: version,
@ -346,7 +346,7 @@ class Package < ApplicationModel
end
begin
data = File.open( location, 'rb' )
data = File.open(location, 'rb')
contents = data.read
rescue => e
raise 'ERROR: ' + e.inspect
@ -355,13 +355,13 @@ class Package < ApplicationModel
end
def self._write_file(file, permission, data)
location = @@root + '/' + file
location = "#{@@root}/#{file}"
# rename existing file
if File.exist?( location )
if File.exist?(location)
backup_location = location + '.save'
logger.info "NOTICE: backup old file '#{location}' to #{backup_location}"
File.rename( location, backup_location )
File.rename(location, backup_location)
end
# check if directories need to be created
@ -369,7 +369,7 @@ class Package < ApplicationModel
(0..(directories.length - 2) ).each {|position|
tmp_path = ''
(1..position).each {|count|
tmp_path = tmp_path + '/' + directories[count].to_s
tmp_path = "#{tmp_path}/#{directories[count]}"
}
next if tmp_path == ''
@ -381,10 +381,10 @@ class Package < ApplicationModel
# install file
begin
logger.info "NOTICE: install '#{location}' (#{permission})"
file = File.new( location, 'wb' )
file.write( data )
file = File.new(location, 'wb')
file.write(data)
file.close
File.chmod( permission.to_i(8), location )
File.chmod(permission.to_i(8), location)
rescue => e
raise 'ERROR: ' + e.inspect
end
@ -392,19 +392,19 @@ class Package < ApplicationModel
end
def self._delete_file(file, _permission, _data)
location = @@root + '/' + file
location = "#{@@root}/#{file}"
# install file
logger.info "NOTICE: uninstall '#{location}'"
if File.exist?( location )
File.delete( location )
if File.exist?(location)
File.delete(location)
end
# rename existing file
backup_location = location + '.save'
if File.exist?( backup_location )
if File.exist?(backup_location)
logger.info "NOTICE: restore old file '#{backup_location}' to #{location}"
File.rename( backup_location, location )
File.rename(backup_location, location)
end
true
@ -413,11 +413,11 @@ class Package < ApplicationModel
class Migration < ApplicationModel
@@root = Rails.root.to_s # rubocop:disable Style/ClassVars
def self.migrate( package, direction = 'normal' )
location = @@root + '/db/addon/' + package.underscore
def self.migrate(package, direction = 'normal')
location = "#{@@root}/db/addon/#{package.underscore}"
return true if !File.exist?( location )
migrations_done = Package::Migration.where( name: package.underscore )
return true if !File.exist?(location)
migrations_done = Package::Migration.where(name: package.underscore)
# get existing migrations
migrations_existing = []
@ -449,26 +449,26 @@ class Package < ApplicationModel
# down
if direction == 'reverse'
done = Package::Migration.find_by( name: package.underscore, version: version )
done = Package::Migration.find_by(name: package.underscore, version: version)
next if !done
logger.info "NOTICE: down package migration '#{migration}'"
load "#{location}/#{migration}"
classname = name.camelcase
Kernel.const_get(classname).down
record = Package::Migration.find_by( name: package.underscore, version: version )
record = Package::Migration.find_by(name: package.underscore, version: version)
if record
record.destroy
end
# up
else
done = Package::Migration.find_by( name: package.underscore, version: version )
done = Package::Migration.find_by(name: package.underscore, version: version)
next if done
logger.info "NOTICE: up package migration '#{migration}'"
load "#{location}/#{migration}"
classname = name.camelcase
Kernel.const_get(classname).up
Package::Migration.create( name: package.underscore, version: version )
Package::Migration.create(name: package.underscore, version: version)
end
}